<template>
	<view style="padding: 30rpx 30rpx 0;">
		<uni-forms ref="form" :rules="rules">
			<uni-forms-item name="username" required>
				<uni-easyinput v-model="formData.username" trim prefixIcon="person" type="text" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item name="nickname">
				<uni-easyinput v-model="formData.nickname" trim prefixIcon="person" type="text" placeholder="请输入昵称" />
			</uni-forms-item>
			<uni-forms-item name="password" required>
				<uni-easyinput v-model="formData.password" trim prefixIcon="eye" type="password" placeholder="请输入密码" />
			</uni-forms-item>
			<uni-forms-item name="pwd2" required>
				<uni-easyinput v-model="formData.pwd2" trim prefixIcon="eye" type="password" placeholder="请再次输入密码" />
			</uni-forms-item>
		</uni-forms>
		<button type="default" @click="register">注册</button>
	</view>
</template>

<script>
	import minxi from '../common/common.mixin.js'
	import rules from './validator.js'
	export default {
		mixins:[minxi],
		data() {
			return {
				rules,
				formData:{
					username:'',
					password:'',
					pwd2:'',
					nickname:''
				}
			}
		},
		methods: {
			register(){
				this.$refs.form.validate().then(res=>{
					console.log('表单数据信息：', res);
					uniCloud.callFunction({
						name:'uni-id-cf',
						data:{
							action:'register',
							params:res
						},
						success: ({result}) => {
							console.log(result)
							if(result.code==0){
								// 处理页面跳转和保存用户信息到store及缓存
								this.loginSuccess(result)
							}else{
								uni.showToast({
									title:result.msg,
									icon:'none'
								})
							}
						},
						fail: (err) => {
							console.log(err)
						}
					})
				}).catch(err =>{
					console.log('表单错误信息：', err);
				})
			}
		},
		onReady()  {
			 this.$refs.form.setRules(this.rules)
		}
	}
</script>

<style>

</style>
